package logic

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"gorm.io/gorm"
	"order_srv/global"
	"order_srv/internal/model"

	"order_srv/internal/svc"
	"order_srv/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type CarDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCarDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CarDetailLogic {
	return &CarDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CarDetailLogic) CarDetail(in *order.CarDetailRequest) (*order.CreateCarResponse, error) {
	// todo: add your logic here and delete this line
	var car model.Car
	tx := global.DB.Where(&model.Car{Model: gorm.Model{ID: uint(in.CarId)}}).First(&car)
	if tx.RowsAffected == 0 {
		return nil, status.Errorf(codes.Internal, "车型不存在")
	}
	return &order.CreateCarResponse{
		Id:    int32(car.ID),
		Name:  car.Name,
		Type:  car.Type,
		Image: car.Image,
		Nums:  car.Nums,
		Price: car.Price,
	}, nil
}
